<?php
// +----------------------------------------------------------------------
// | makePHP
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**
 * 功能: 公用函数
 * 创建日期:Wed Oct 29 12:04:04 CST 2008
 * 最后更新:
 * 作者: sanshi
 */

/**
 * 错误处理函数，页面跳转
 * @author sanshi
 * @param string $page  跳转页面
 * @param string $msg   提示信息
 * @param  int   $h     跳转时间
 */
function jumpPage($jump_page,$msg='',$h=0)
{
	//echo $jump_page.time();exit();
	
	//$msg = empty($msg)?'':"?msg=".base64_encode($msg);
	$msg = urlencode($msg);
	$msg = empty($msg)?'':"?msg={$msg}";
	$url=$jump_page.$msg;
	//echo $url;exit();
	if(!@header("Location:".$url))
	{
	   echo "<meta http-equiv=refresh content='{$h};url=".$url."'>";
	}
	
	exit();
}

/**
 * 错误信息处理
 * @name jumpError
 * @param  int	error_no 	错误号
  * @param  int	$jump_url 	转到的url
 * @return
 * @access public
 * @see
 * @version	1.0.0 (Wed Oct 29 11:06:28 CST 2008)
 * @author	sanshi
 */
function jumpError($error_no,$jump_url="",$is_admin=1)
{
	if($error_no<= -2000)
	{
		//采取js输出
		alertMsg($error_no,$jump_url);
	}else{
		global $error_object;
		//取得错误信息
		$msg = $error_object->getErrorMsg($error_no);
		if($is_admin)
		{
			$page = empty($jump_url)?ADMIN_HOST_PATH.'error.php':$jump_url;
		}else{
			$page = empty($jump_url)?HOST_PATH.'error.php':$jump_url;
		}
		//采取页面跳转
		jumpPage($page,$msg,$h=0);
	}
}
//信息提示，返回项目首页
function goToIndex($error_no)
{
	global $error_object;
	//取得错误信息
	$msg = $error_object->getErrorMsg($error_no);
	echo "<script>";
	echo "alert(' {$msg} ');";
	echo "window.location='".HOST_NAME."/index.php';";
	echo "</script>";
	exit();
}
//信息提示，如果给跳转页面就跳到指定页，如果不提供，直接跳转到上一页
function alertMsg($error_no,$url = '')
{
	global $error_object;
	//取得错误信息
	$msg = $error_object->getErrorMsg($error_no);
	echo "<script>";
	echo "alert(' {$msg} ');";
	echo true == empty($url) ? "history.go(-1);" :  "window.location='{$url}';";
	echo "</script>";
	exit();
}
//信息提示，如果给跳转页面就跳到指定页，如果不提供，直接跳转到上一页
function alertMsg2($msg,$url = '')
{
	echo "<script>";
	echo "alert(' {$msg} ');";
	echo true == empty($url) ? "history.go(-1);" :  "window.location='{$url}';";
	echo "</script>";
	exit();
}
function alertMsg3($msg,$url = '')
{
	echo "<script>";
	//echo "self.parent.tb_remove();";
	echo "alert(' {$msg} ');";
	echo true == empty($url) ? "history.go(-1);" :  "window.location='{$url}';";
	echo "</script>";
	exit();
}

function getUserIP()
{
	if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
		$user_ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
	} else {
		$user_ip=$_SERVER["REMOTE_ADDR"];
	}
	return $user_ip;
}
//获取动态码
function get_code()
{
	$profix = 'rand_';
	$mc = Factory::getMemcached();
	while(1)
	{
		$uuid = get_uuid();
		$uuids = $profix.$uuid;
		$temp = $mc->get($uuids);
		if(empty($temp))
		{
			$mc->set($uuids,"sanshi key",3600);
			return $uuid;
		}
	}
}
//网站动态验证码检测
function check_code($code)
{
	$mc = Factory::getMemcached();
	$profix = 'rand_';
	$uuid = $profix.$code;
	$temp = $mc->get($uuid);
	if(empty($temp))
	{
		return false;
	}
	$mc->delete($uuid);
	return true;
}
//获取鉴权值 合作方传递过来的
function get_app_key($key,$user_id,$user_name,$icpcode)
{
	return substr(md5($key.$user_id.$user_name.$icpcode),0,24);
}
//回调给合作方的鉴权
function set_app_key($key,$user_id,$id5_id,$icpcode)
{
	return substr(md5($key.$user_id.$icpcode.$id5_id),0,24);
}
//拼接返回url数组
function get_call_back_url($icpcode,$user_id,$id5_id,$rtncode)
{
	$icp_arr = get_icpcode_info($icpcode);
	if(!empty($icp_arr))
	{
		$key_code = set_app_key($icp_arr['key'],$user_id,$id5_id,$icpcode);
		$temp = array();
		//返回验证号
		$temp['result'] = $rtncode;
		//鉴权码
		$temp['key_code'] = $key_code;
		//用户id
		$temp['user_id'] = $user_id;
		//id5 id
		$temp['stid'] = $id5_id;
		return http_build_query($temp);
	}
}
function get_uuid()
{
	list($usec, $sec) = explode(" ", microtime());
	return $usec.$sec;

}



//计算权限值 用户突破32位限制
function parse_power($user_power,$method_power)
{
	return  $user_power & $method_power;
}

/**
 *
 *
 * @author slash
* @copyright slash, 19 四月, 2010
 * @package default
 **/

/**
*将验证码存储进入MEMCACHE
**/
function mem_code($mob,$time=0){
	global $memcacheConfig;
	$mc = Factory::getMemcached();
	$profix = 'code_';
	//通过邮箱和手机号与时间生成唯一标示
	//$code_arr = get_shot_arr($value.time());
	$i = 0;

	while ($i<99) {
		$code = get_int_code();
		$result = $mc->get($profix.$code);
		if(empty($result))
		{
			if($mc->set($profix.$code,$mob,$time))
			{
				return $code;
			}
			break;
		}
		$i++;
	}
	return false;

}

function get_int_code(){
	$time = time();
	return $code = substr($time,-4).rand(10,99);
}

/**
 * 取得key对应的值
 *
 * @param unknown_type $key    键值
 * @return unknown
 */
function get_key_value($key)
{
	global $memcacheConfig;
	$mc = Factory::getMemcached();
	//$mc->addServer($memcacheConfig[0]['server'],$memcacheConfig[0]['port']);
	$profix = 'code_';
	$result = $mc->get($profix.$key);
	return isset($result) ? $result : false;
}

/**
 * 获得合作伙伴信息
 *
 * @param 数值 $icpcode
 */
function get_coop_name($icpcode)
{
	global $coop_arr;
	return $coop_arr[$icpcode]['name'];
}
//获取 icpcode 用户信息
function get_icpcode_info($icpcode)
{
	global $coop_arr;
	return isset($coop_arr[$icpcode]) ? $coop_arr[$icpcode] : array();
}

// 浏览器友好的变量输出
function dump($var, $echo=true,$label=null, $strict=true)
{
    $label = ($label===null) ? '' : rtrim($label) . ' ';
    if(!$strict) {
        if (ini_get('html_errors')) {
            $output = print_r($var, true);
            $output = "<pre>".$label.htmlspecialchars($output,ENT_QUOTES)."</pre>";
        } else {
            $output = $label . " : " . print_r($var, true);
        }
    }else {
        ob_start();
        var_dump($var);
        $output = ob_get_clean();
        if(!extension_loaded('xdebug')) {
            $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
            $output = '<pre>'. $label. htmlspecialchars($output, ENT_QUOTES). '</pre>';
        }
    }
    if ($echo) {
        echo($output);
        return null;
    }else
        return $output;
}

// 循环创建目录
function mk_dir($dir, $mode = 0777) {
    if (is_dir($dir) || @mkdir($dir, $mode))
        return true;
    if (!mk_dir(dirname($dir), $mode))
        return false;
    return @mkdir($dir, $mode);
}


?>
